package com.xd.lib.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xd.lib.R;
import com.xd.lib.tool.util.ActivityUtil;
import com.xd.lib.tool.util.SoundUtil;

/**
 * 对话框
 * Created by Administrator on 2015/6/12.
 */
public class DialogUtil {

    /**
     * 对话框
     */
    private static AlertDialog alertDialog;

    /**
     * 显示自定义对话框
     * @param context
     * @param message
     * @param listener
     */
    public static void showDialog1(final Context context,
                                  String message,
                                  final IDialogButtonClickListener listener){
        View view = ActivityUtil.getView(context, R.layout.dialog1_layout);

        ImageButton btnCancel = (ImageButton)view.findViewById(R.id.btn_dialog_cancel);
        ImageButton btnOK = (ImageButton)view.findViewById(R.id.btn_dialog_ok);
        TextView txtMessage = (TextView)view.findViewById(R.id.txt_dialog_message);

        txtMessage.setText(message);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭对话框
                if (alertDialog != null){
                    alertDialog.cancel();
                }
                SoundUtil.playSound(context, SoundUtil.INDEX_SOUND_CANCEL);
            }
        });

        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭对话框
                if (alertDialog != null){
                    alertDialog.cancel();
                }

                SoundUtil.playSound(context, SoundUtil.INDEX_SOUND_OK);

                //事件回调
                if (listener != null){
                    listener.onClick();
                }
            }
        });

        /**
         * 使用透明样式，以消除对话框边界的边线
         */
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Theme_Transparent);
        builder.setView(view);

        alertDialog = builder.create();
        alertDialog.show();
    }
}
